package com.dong.controller;
import com.dong.mybatis.entity.Zone2AlarmRecord;
import com.dong.mybatis.mapper.Zone1AlarmRecordMapper;
import com.dong.mybatis.mapper.Zone1AlarmThresholdsMapper;
import com.dong.mybatis.mapper.Zone2AlarmRecordMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.dong.mybatis.entity.Zone3AlarmRecord;
import com.dong.mybatis.mapper.Zone3AlarmRecordMapper;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/alarmsData")
public class alarmsDataController {
    @Resource
    Zone1AlarmThresholdsMapper zone1AlarmThresholdsMapper;
    @Resource
    Zone3AlarmRecordMapper zone3AlarmRecordMapper;
    @Resource
    Zone2AlarmRecordMapper zone2AlarmRecordMapper;
    @Resource
    Zone1AlarmRecordMapper zone1AlarmRecordMapper;

    @PostMapping("ConfigForm")
    // 关键：用 @RequestBody 注解接收前端的 JSON 数据，绑定到 Map 中
    public String ConfigForm(@RequestBody Map<String, Object> formData) {
        // 遍历formData中所有key（假设key格式为item1~item5）
        for (Map.Entry<String, Object> entry : formData.entrySet()) {
            String key = entry.getKey();
            // 只处理item1到item5的配置
            if (key.startsWith("item") && key.matches("item[1-5]")) {
                // 转换为子Map（包含min和max）
                Map<String, String> item = (Map<String, String>) entry.getValue();
                // 提取min和max
                String minStr = item.get("min");
                String maxStr = item.get("max");

                // 转为数字（处理可能的格式异常）
                try {
                    double min = Double.parseDouble(minStr);
                    double max = Double.parseDouble(maxStr);

                    // 这里可以执行后续操作，比如更新数据库
                    // 示例：根据item编号（如item1对应id=1）更新
                    int id = Integer.parseInt(key.replace("item", "")); // 提取1-5作为id
                    zone1AlarmThresholdsMapper.updateMinMaxById(min, max, id);

                } catch (NumberFormatException e) {
                    // 处理数字转换失败的情况
                    return "参数格式错误：min或max不是有效的数字";
                }
            }
        }
        return "配置更新成功";
    }

    @GetMapping("/PartOneAlarmsData")
    public List<Zone3AlarmRecord> PartOneAlarmsData(String zoneId) {
        return zone3AlarmRecordMapper.getAll();
    }

    @GetMapping("/PartTwoAlarmsData")
    public List<Zone2AlarmRecord> PartTwoAlarmsData(String zoneId) {
        return zone2AlarmRecordMapper.getAll();
    }

    @GetMapping("/PartThreeAlarmsData")
    public List<Zone3AlarmRecord> PartThreeAlarmsData(String zoneId) {
        return zone3AlarmRecordMapper.getAll();
    }
}



/**
 * <p>
 * zone1区域简化版报警记录表
 * </p>
 *
 * @author dong
 * @since 2025-09-08
 */
@Getter
@Setter
@ToString class ZoneAlarmRecord implements Serializable {

    private static final long serialVersionUID = 1L;


    private Long id;

    private String parameter;

    private Double actualValue;

    private Double thresholdMin;

    private Double thresholdMax;

    private LocalDateTime alarmTime;

    private String alarmDesc;
}
